<?php
/**
 * PatientSignalFile model class file.
 *
 * @package   models
 * @since     1.0
 * @filesource
 */

/**
 * This is the model class for table "{{patient_signal_file}}".
 * A PatientSignalFile is an uploaded file for a specific patient signal record.
 * A PatientSignal record may relate to multiple PatientSignalFile.
 * Each PatientSignalFile stores the metadata for a file on the file system.
 * This is a DAO class which is used by PatientSignalController or other controllers
 * to operate data in database or return data in a specified format.
 *
 * The followings are the available columns in table '{{patient_signal_file}}':
 * @property integer $id
 * @property integer $patientSignalId
 * @property string $filename
 *
 * The followings are the available model relations:
 * @property PatientSignal $patientSignal - a PatientSignal object related to this patient signal file.
 */
class PatientSignalFile extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return PatientSignalFile the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * Returns the table name related to this model class.
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{patient_signal_file}}';
	}

	/**
	 * Returns the validation rules.
	 * The rules will be used when requesting validation by call self.validate().
	 * Many class method will call self.validate(), eg: CActiveRecord.save().
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
		array('patientSignalId, filename', 'required'),
		array('patientSignalId', 'numerical', 'integerOnly'=>true),
		array('filename', 'length', 'max'=>255),
		);
	}

	/**
	 * Returns the relationship between this Model class and other Model class
	 * based on foreign key constraint between corresponding tables.
	 * The relationships involved in this application involves HAS_MANY, HAS_ONE, BELONGS_TO,
	 * please refer to Yii document for details.
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			// one patient singal file is related to one patient signal.
			'patientSignal' => array(self::BELONGS_TO, 'PatientSignal', 'patientSignalId'),
		);
	}

	/**
	 * Returns the labels for each column variables.
	 * This will be used to render label on page.
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'patientSignalId' => 'Patient Signal',
			'filename' => 'Filename',
		);
	}
	
	/**
	 * Delete the record and also delete the files on the disk.
	 * This metod will rewrite the delete method inherited from CActiveRecord to delete patient signal file store on server.
	 * @see CActiveRecord::delete()
	 */
	public function delete(){
		if(!empty($this->filename)){
			$filePath=$this->patientSignal->signalPath.DIRECTORY_SEPARATOR.$this->filename;
			if(file_exists($filePath)){
				@unlink($filePath);
			}
		}
		parent::delete();
	}
}